import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '/',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/password',
    name: 'password',
    component: () => import('../views/Password')
  },
  {
    path: '/person',
    name: 'person',
    component: () => import('../views/Person')
  },
  {
    path: '/schoolRegister',
    name: 'schoolRegister',
    component: () => import('../views/school/schoolReg')
  },
  {
    path: '/campusRegister',
    name: 'campusRegister',
    component: () => import('../views/campus/campusReg')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
  {
    path: '/school',
    name: 'school',
    component: () => import('../views/ManageForSchool.vue'),
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/school/SchoolHome')
      },
      {
        path: 'manageafc',
        name: 'manageApplicationForCampus',
        component: () => import('../views/school/ManageApplicationForCampus')
      },
      {
        path: 'manageallcampus',
        name: 'manageallcampus',
        component: () => import('../views/school/ManageAllCampus')
      },
      {
        path: 'unpasscampus',
        name: 'unpasscampus',
        component: () => import('../views/school/UnPassCampus')
      },
      {
        path: 'manageDisband',
        name: '社团解散管理',
        component: () => import('../views/school/DisbandManage')
      },
      {
        path: 'manageAct',
        name: '社团活动管理',
        component: () => import('../views/school/ActivityManage')
      },
      {
        path: 'manageMoney',
        name: '社团活动管理',
        component: () => import('../views/school/MoneyManage')
      },
      {
        path: 'message',
        name: '留言',
        component: () => import('../views/campus/Message')
      }
    ]
  },
  {
    path: '/master',
    name: 'master',
    component: () => import('../views/ManageForMaster.vue'),
    children:[
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/master/CampusHomeForMaster')
      },
      {
        path: 'mycampus',
        name: 'mycampus',
        component: () => import('../views/campus/MyCampus')
      },
      {
        path: 'member',
        name: 'member',
        component: () => import('../views/master/Member')
      },
      {
        path: 'joinmanage',
        name: '加入社团申请管理',
        component: () => import('../views/master/JoinManage')
      },
      {
        path: 'actappli',
        name: 'actappli',
        component: () => import('../views/campus/ActivityAppli')
      },
      {
        path: 'allcampus',
        name: 'allcampus',
        component: () => import('../views/campus/AllCampus')
      },
      {
        path: 'update',
        name: '修改社团信息',
        component: () => import('../views/campus/UpdateCampus')
      },
      {
        path: 'exitManage',
        name: '退出社团申请管理',
        component: () => import('../views/master/ExitCampus')
      },
      {
        path: 'myAct',
        name: '我的活动',
        component: () => import('../views/campus/MyActivity')
      },
      {
        path: 'money',
        name: '资金申请',
        component: () => import('../views/master/Money')
      },
      {
        path: 'message',
        name: '留言',
        component: () => import('../views/campus/Message')
      },
      {
        path: 'campusMessage',
        name: '社团留言',
        component: () => import('../views/campus/CampusMessage')
      }
    ]
  },
  {
    path: '/campus',
    name: 'campus',
    component: () => import('../views/ManageForCampus.vue'),
    children:[
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/campus/CampusHome')
      },
      {
        path: 'all',
        name: '全部社团',
        component: () => import('../views/campus/AllCampus')
      },
      {
        path: 'mycampus',
        name: 'mycampus',
        component: () => import('../views/campus/MyCampus')
      },
      {
        path: 'message',
        name: '留言',
        component: () => import('../views/campus/Message')
      }
    ]
  },
  {
    path: '/simple',
    name: 'simple',
    component: () => import('../views/ManageForSimple.vue'),
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/campus/CampusHome')
      },
      {
        path: 'all',
        name: 'all',
        component: () => import('../views/campus/AllCampus')
      },
      {
        path: 'create',
        name: 'create',
        component: () => import('../views/campus/CreateCampus')
      },
      {
        path: 'mycampus',
        name: 'mycampus',
        component: () => import('../views/campus/MyCampus')
      },
      {
        path: 'myappliforjc',
        name: '加入社团申请页面',
        component: () => import('../views/campus/MyAppliForJC')
      },
      {
        path: 'myappliforcreatecam',
        name: '成立社团申请页面',
        component: () => import('../views/campus/MyAppliForCreateC')
      },
      {
        path: 'message',
        name: '留言',
        component: () => import('../views/campus/Message')
      }
    ]
  },
  {
    path: '/member',
    name: 'member',
    component: () => import('../views/ManageForCampus.vue'),
    children: [
      {
        path: 'member',
        name: 'member',
        component: () => import('../views/member/Member')
      },
      {
        path: 'actappli',
        name: 'actappli',
        component: () => import('../views/campus/ActivityAppli')
      },
      {
        path: 'myAct',
        name: '我的活动',
        component: () => import('../views/campus/MyActivity')
      },
      {
        path: 'message',
        name: '留言',
        component: () => import('../views/campus/Message')
      },
      {
        path: 'campusMessage',
        name: '社团留言',
        component: () => import('../views/campus/CampusMessage')
      }
    ]
  },
  {
    path: '/visitor',
    name: '游客',
    component: () => import('../views/Visitor.vue'),
    children:[
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/campus/CampusHome')
      },
      {
        path: 'all',
        name: '全部社团',
        component: () => import('../views/campus/AllCampus')
      },
      {
        path: 'message',
        name: '留言',
        component: () => import('../views/campus/Message')
      }
      
    ]
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}



router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
  store.commit("setPath")

  // 未找到路由的情况
  if (!to.matched.length) {
    const storeMenus = localStorage.getItem("menus")
    if (storeMenus) {
      next("/404")
    } else {
      // 跳回登录页面
      next("/login")
    }
  }
  // 其他的情况都放行
  next()

})

export default router
